package com.wk.ums.listener;

import com.wk.common.constant.mq.ItemExchange;
import com.wk.common.constant.mq.ItemQueue;
import com.wk.common.constant.mq.ItemRoutingKey;
import com.wk.ums.entity.Member;
import com.wk.ums.mapper.MemberMapper;
import com.wk.ums.vo.UserIntegralVO;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 监听用户相关操作
 */
@Component
public class UserListener {

    @Autowired
    private MemberMapper memberMapper;

    /**
     * 订单创建成功 增加用户的成长值和普通积分
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = ItemExchange.ORDER_EXCHANGE, ignoreDeclarationExceptions = "true"),
            value = @Queue(value = ItemQueue.INTEGRAL_INCREASE_QUEUE, durable = "true"),
            key = ItemRoutingKey.INTEGRAL_INCREASE
    ))
    public void increaseIntegral(UserIntegralVO userIntegralVO){
        Member member = new Member();
        member.setId(userIntegralVO.getMemberId());
        member.setIntegration(userIntegralVO.getIntegration());
        member.setGrowth(userIntegralVO.getGrowth());
        memberMapper.updateIntegralById(member);
    }

}
